/*
 * ChoosePayWayProvider.java
 * business
 *
 * Created by chentao on 2017/5/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.ShopPayWays;
import com.yidiandao.business.data.bean.ITag;
import com.yidiandao.business.data.bean.PayWay;
import com.yidiandao.business.ui.widget.view.RatioLayout;
import com.yidiandao.business.ui.widget.view.TagFlowLayout;
import com.yidiandao.business.ui.widget.view.TagView;
import com.yidiandao.business.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 选择支付方式Provider
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/5/2 0002 15:24
 */
public class ChoosePayWayProvider extends ItemViewProvider<ShopPayWays, ChoosePayWayProvider.ViewHolder> {

    private Activity mActivity;

    private OnTagClickListener mTagClickListener;

    public ChoosePayWayProvider(Activity activity, OnTagClickListener clickListener) {
        mActivity = activity;
        mTagClickListener = clickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_choose_pay_way, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final ShopPayWays shopPayWays) {

        holder.tvTitle.setText(shopPayWays.getSupplierName());

        // 数据转换
        List<ITag> tags = new ArrayList<>();
        List<PayWay> payWays = shopPayWays.getPayWays();
        for (PayWay payWay : payWays) {
            if (payWay.isSelected()) {
                holder.tvPayWayHint.setText(payWay.getText());
            }
            tags.add(payWay);
        }

        holder.tagFlowLayout.setTagState(TagView.PAY_WAY);
        holder.tagFlowLayout.setTagsData(tags);
        holder.tagFlowLayout.setTagClickHelperListener(new TagFlowLayout.TagClickHelperListener() {
            @Override
            public boolean onTagClick(TagView tagView) {
                PayWay tagData = (PayWay) tagView.getTagData();
                holder.tvPayWayHint.setText(tagData.getText());
                return mTagClickListener.onTagClick(shopPayWays, tagData);
            }
        });

        // 图片显示
        List<String> images = shopPayWays.getImages();
        String image1 = null;
        String image2 = null;
        String image3 = null;
        try {
            image1 = images.get(0);
            holder.ratioLayout1.setVisibility(View.VISIBLE);
            ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivImage1, image1);

            image2 = images.get(1);
            holder.ratioLayout2.setVisibility(View.VISIBLE);
            ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivImage2, image2);

            image3 = images.get(2);
            holder.ratioLayout3.setVisibility(View.VISIBLE);
            ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivImage3, image3);
        } catch (IndexOutOfBoundsException | NullPointerException e) {
            // 店铺图片不足3张
            if (image1 == null) holder.ratioLayout1.setVisibility(View.INVISIBLE);
            if (image2 == null) holder.ratioLayout2.setVisibility(View.INVISIBLE);
            if (image3 == null) holder.ratioLayout3.setVisibility(View.INVISIBLE);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;
        private ImageView ivImage1;
        private ImageView ivImage2;
        private ImageView ivImage3;
        private RatioLayout ratioLayout1;
        private RatioLayout ratioLayout2;
        private RatioLayout ratioLayout3;
        private TagFlowLayout tagFlowLayout;
        private TextView tvPayWayHint;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            ivImage1 = (ImageView) itemView.findViewById(R.id.iv_image_1);
            ivImage2 = (ImageView) itemView.findViewById(R.id.iv_image_2);
            ivImage3 = (ImageView) itemView.findViewById(R.id.iv_image_3);
            ratioLayout1 = (RatioLayout) itemView.findViewById(R.id.ratio_layout_1);
            ratioLayout2 = (RatioLayout) itemView.findViewById(R.id.ratio_layout_2);
            ratioLayout3 = (RatioLayout) itemView.findViewById(R.id.ratio_layout_3);
            tagFlowLayout = (TagFlowLayout) itemView.findViewById(R.id.tag_flow_layout);
            tvPayWayHint = (TextView) itemView.findViewById(R.id.tv_pay_way_hint);
        }
    }

    public interface OnTagClickListener {
        boolean onTagClick(ShopPayWays shopPayWays, PayWay payWay);
    }
}

